package com.example.servlet;

import com.example.dao.CoachDao;
import com.example.pojo.Coach;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/doCoach")
public class CoachServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CoachDao dao=new CoachDao();
        PrintWriter out = resp.getWriter();
        switch (req.getParameter("methods"))
        {
            case "add":
                Coach coach=new Coach();
                coach.setName(req.getParameter("name"));
                coach.setAge(Integer.valueOf(req.getParameter("age")));
                coach.setSex(req.getParameter("sex"));
                coach.setPhone(req.getParameter("phone"));
                coach.setPass(req.getParameter("pass"));
                if(dao.addCoach(coach))
                out.println("<h3>添加教练成功</h3>");
                else out.println("<h3>添加教练失败</h3>");
                break;
            case "setpass":
                if(dao.setCoachPass(req.getParameter("phone"),
                        req.getParameter("pass")))
                    out.println("<h3>修改密码成功</h3>");
                else out.println("<h3>修改密码失败</h3>");
                break;
            case "delete":
                if(dao.deleteCoach(req.getParameter("phone")))
                    out.println("<h3>删除教练成功</h3>");
                else out.println("<h3>删除教练失败</h3>");
                break;
            default:
                break;
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
